package com.epam.rd.my_classLoader;

import java.io.IOException;
import java.util.Arrays;

import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;


public class JavaFileCompiler {
	
	public static void compileJava(String file) throws IOException{
	     JavaCompiler compiler = ToolProvider
                 .getSystemJavaCompiler();
     DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
     StandardJavaFileManager fileManager = compiler
                 .getStandardFileManager( diagnostics, null, null );
     Iterable<? extends JavaFileObject> compilationUnits = fileManager
                 .getJavaFileObjectsFromStrings( Arrays
                                 .asList( "./TestModule.java" ) );
     JavaCompiler.CompilationTask task = compiler.getTask( null,
                 fileManager, diagnostics, null, null,
                 compilationUnits );
     boolean success = task.call();
     fileManager.close();
     System.out.println( "Compile"+file+" is Success:!!! " + success );
	}
}
